﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL.Classes;
using DAL.Connect;

public partial class UserControl_UCBaiDang : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateControls();
        }
    }
    private void PopulateControls()
    {
        int howManyPages = 0;
        string ChuDeID = Request.QueryString["ChuDeID"] ?? "";
        string trang = Request.QueryString["Trang"] ?? "1";
        string theLoai = Request.QueryString["TheLoaiID"] ?? "";
        string searchString = Request.QueryString["ChuoiTimKiem"] ?? "";
        string maTaiKhoan = Request.QueryString["MaTaiKhoan"] ?? "";
        string firstPageUrl = "";
        string pagerFormat = "";
        if (theLoai.CompareTo("2") != 0)
        {
            theLoai = "1";
        }
        if (searchString != "")
        {
            dlAlbumList.DataSource = BaiDang.TimTheoTenVaTheLoai(theLoai, searchString, trang, out howManyPages);
            dlAlbumList.DataBind();
            int CountSearch = BaiDang.DemBaiDangTheoKetQuaTimKiem(theLoai, searchString);
            lbResults.Text = "Kết quả có tất cả '" + CountSearch + "' Album cho từ khóa '" + searchString.ToString() + "'";
            firstPageUrl = Link.DenTrangTimKiem(searchString);
            pagerFormat = Link.DenTrangTimKiem(searchString, "{0}");
        }
        else
        {
            if (ChuDeID != "")
            {
                dlAlbumList.DataSource = BaiDang.BaiDangThuocChuDe_TheLoai(ChuDeID, theLoai, trang, out howManyPages);
                dlAlbumList.DataBind();
                int DemBDChuDeID = BaiDang.DemBaiDangTheoChuDeVaTheLoai(ChuDeID, theLoai);
                lbResults.Text = "Có tất cả '" + DemBDChuDeID + "' Album cho chuyên mục này!";
                firstPageUrl = Link.DenChuDe(theLoai, ChuDeID);
                pagerFormat = Link.DenChuDe(theLoai, ChuDeID, "{0}");
            }
            else if (maTaiKhoan != "")
            {
                dlAlbumList.DataSource = BaiDang.BaiDangTheoTaiKhoan_TheLoai(maTaiKhoan, trang, out howManyPages);
                dlAlbumList.DataBind();
                int demTheLoai1 = BaiDang.DemBaiDangTheoMaTaiKhoan_TheLoai(maTaiKhoan, "1");
                int demTheLoai2 = BaiDang.DemBaiDangTheoMaTaiKhoan_TheLoai(maTaiKhoan, "2");
                lbResults.Text = "Tổng số Bài đăng cho hình ảnh là: '" + demTheLoai1 + "' và Video là: '" + demTheLoai2 + "'";
                firstPageUrl = Link.DenTaiKhoan(maTaiKhoan);
                pagerFormat = Link.DenTaiKhoan(maTaiKhoan, "{0}");
            }
        }
        PagerBottom.Show(int.Parse(trang), howManyPages, firstPageUrl, pagerFormat, true);
    }
}
